package edu.mis.modules.role;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.render.JsonRender;

import edu.mis.model.Role;

public class RoleController extends Controller {

	public void index() {
		render("role.html");
	}
	public void list() {
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		Page<Role> data = Role.dao.paginate(page, rows);
		setAttr("rows", data.getList());
		setAttr("total", data.getTotalRow());
		renderJson();
	}

	public void add() throws Exception {
		Model<Role> role = getModel(Role.class,"");
		if (role.save()) {
			setAttr("success",true);
			setAttr("msg","添加角色成功!");
		}else{
			setAttr("success",false);
			setAttr("msg","添加角色失败!");
		}
		renderJson();
	}
	
	public void edit() throws Exception {
		Model<Role> role = this.getModel(Role.class,"");
		if (role.update()) {
			setAttr("success",true);
			setAttr("msg","修改角色成功!");
		}else{
			setAttr("success",false);
			setAttr("msg","修改角色失败!");
		}
		renderJson();
	}

	public void delete() throws Exception {
		String ids=getPara("ids");
		if(Role.dao.deleteBatch(ids)){ 
			setAttr("success",true);
			setAttr("msg","删除角色成功!");
		}else{
			setAttr("success",true);
			setAttr("msg","删除角色失败!");
		}
		renderJson();
	}
	/**
	 * 得到指定用户的角色列表
	 */
	public void getUserRole(){
		renderJson(Role.dao.getUserRole(getPara("userId")));
	}
	/**
	 * 得到指定角色的权限
	 */
	public void getRolePermission(){
		render(new JsonRender(Role.dao.getRolePermission(getPara("id"))).forIE());
	}
	/**
	 * 批量更新指定用户的角色
	 */
	public void saveUserRole(){
		String userId=getPara("userId");
		String roleId=getPara("roleId");
		Role.dao.saveUserRole(userId, roleId);
		renderJson();
	}
	/**
	 * 批量更新指定角色的权限
	 */
	public void saveRolePermission(){
		String roleId=getPara("roleId");
		String resourceIds=getPara("resourceIds");
		Role.dao.saveRolePermission(roleId, resourceIds);
		renderJson();
	}
}
